/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Robin Chutaux
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package custom.base.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.core.content.ContextCompat;

import custom.base.R;
import custom.base.utils.DensityUtil;
import custom.base.utils.TxtUtil;


/**
 * RippleView custom layout
 * <p/>
 * Custom Layout that allows to use Ripple UI pattern above API 21
 *
 * @author luozisong
 * @version 2016.0303
 */
public class TitleNormal extends RelativeLayout {

    public TitleNormal(Context context) {
        super(context);
    }

    public TitleNormal(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TitleNormal(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    /**
     * 以下为配置文件里的参数
     */
    private String text = "";
    private int icon1 = 0, icon2 = 0, icon3 = 0;
    private int leftIcon = 0;
    private String leftBtn;
    private String textBtn1;
    private String textBtn2;
    /**
     * 1.黑色 2.主题蓝色
     */
    private int btn1Color = 1, btn2Color = 1, leftBtnColor = 1;
    /**
     * 标题栏背景色和文字色
     */
    private String bgColor = null, titleColor = null;
    /**
     * 显示返回按钮
     */
    private boolean showBack = true;
    private boolean showBottomLine = true;

    private View view;
    private View line;
    private ImageView ivBack;
    private LinearLayout linearLayout;
    private RelativeLayout llRoot;
    private LinearLayout llLeft;
    private ImageView ivIc1, ivIc2, ivIc3;
    private TextView tvLeftBtn;
    private TextView tvBtn1;
    private TextView tvBtn2;
    private TextView name;

    /**
     * Method that initializes all fields and sets listeners
     *
     * @param context Context used to create this view
     * @param attrs   Attribute used to initialize fields
     */
    private void init(final Context context, final AttributeSet attrs) {

        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleNormal);
        text = typedArray.getString(R.styleable.TitleNormal_title_name);//default white
        icon1 = typedArray.getResourceId(R.styleable.TitleNormal_title_icon1, 0);//default 0
        icon2 = typedArray.getResourceId(R.styleable.TitleNormal_title_icon2, 0);//default 0
        icon3 = typedArray.getResourceId(R.styleable.TitleNormal_title_icon3, 0);//default 0
        leftIcon = typedArray.getResourceId(R.styleable.TitleNormal_title_left_icon, 0);//default 0
        leftBtn = typedArray.getString(R.styleable.TitleNormal_title_left_btn);//default 0
        textBtn1 = typedArray.getString(R.styleable.TitleNormal_title_text_btn1);//default 0
        textBtn2 = typedArray.getString(R.styleable.TitleNormal_title_text_btn2);//default 0
        leftBtnColor = typedArray.getInteger(R.styleable.TitleNormal_title_left_btn_color, leftBtnColor);//default 0
        btn1Color = typedArray.getInteger(R.styleable.TitleNormal_title_text_btn1_color, btn1Color);//default 0
        btn2Color = typedArray.getInteger(R.styleable.TitleNormal_title_text_btn2_color, btn2Color);//default 0
        bgColor = typedArray.getString(R.styleable.TitleNormal_title_bgColor);//default 0
        titleColor = typedArray.getString(R.styleable.TitleNormal_title_color);//default 0
        showBack = typedArray.getBoolean(R.styleable.TitleNormal_title_show_back, showBack);//default 0
        showBottomLine = typedArray.getBoolean(R.styleable.TitleNormal_title_show_bottom_line, showBottomLine);//default 0
        typedArray.recycle();

        view = LayoutInflater.from(context).inflate(R.layout.view_title_normal, this);
        name = (TextView) view.findViewById(R.id.top_bar_title_name);
        linearLayout = (LinearLayout) view.findViewById(R.id.view_title_normal_layout);
        llRoot = (RelativeLayout) view.findViewById(R.id.view_title_normal_root);
        llLeft = (LinearLayout) view.findViewById(R.id.view_title_normal_left_layout);
        ivBack = view.findViewById(R.id.top_bar_back);
        line = view.findViewById(R.id.view_title_normal_line);

        name.setText(text);
        if (!TxtUtil.isEmpty(leftBtn)) {
            tvLeftBtn = getLeftTextBtn(leftBtn, 1, leftBtnColor);
            llLeft.addView(tvLeftBtn);
            ivBack.setVisibility(GONE);
        }
        if (leftIcon != 0) {
            showBack = true;
            ivBack.setImageResource(leftIcon);
            ivBack.setPadding(DensityUtil.dip2px(getContext(), 12), DensityUtil.dip2px(getContext(), 12)
                    , DensityUtil.dip2px(getContext(), 12), DensityUtil.dip2px(getContext(), 12));
        } else {
            ivBack.setPadding(DensityUtil.dip2px(getContext(), 12), DensityUtil.dip2px(getContext(), 17)
                    , DensityUtil.dip2px(getContext(), 17), DensityUtil.dip2px(getContext(), 12));
        }
        if (showBack) {
            ivBack.setVisibility(VISIBLE);
        } else {
            ivBack.setVisibility(GONE);
        }
        if (!TxtUtil.isEmpty(textBtn1)) {
            tvBtn1 = getTextBtn(textBtn1, 1, btn1Color);
            linearLayout.addView(tvBtn1);
        }
        if (!TxtUtil.isEmpty(textBtn2)) {
            tvBtn2 = getTextBtn(textBtn2, 2, btn2Color);
            linearLayout.addView(tvBtn2);
        }
        if (icon1 != 0) {
            ivIc1 = getItemView(icon1, 1);
            linearLayout.addView(ivIc1);
        }
        if (icon2 != 0) {
            ivIc2 = getItemView(icon2, 2);
            linearLayout.addView(ivIc2);
        }
        if (icon3 != 0) {
            ivIc3 = getItemView(icon3, 3);
            linearLayout.addView(ivIc3);
        }

        if (showBottomLine) {
            line.setVisibility(VISIBLE);
        } else {
            line.setVisibility(GONE);
        }

        if (!TxtUtil.isEmpty(bgColor)) {
            llRoot.setBackgroundColor(Color.parseColor(bgColor));
        } else {
            llRoot.setBackgroundColor(Color.WHITE);
        }
        if (!TxtUtil.isEmpty(titleColor)) {
            name.setTextColor(Color.parseColor(titleColor));
        }
        this.setClickable(true);

    }

    public void setText(String text) {
        if (text != null && text.trim().length() > 0) {
            this.text = text;
            name.setText(text);
        }
    }

    private ImageView getItemView(int resource, int index) {
        @LayoutRes int r = 0;
        switch (index) {
            case 1:
                r = R.layout.view_title_normal_item1;
                break;
            case 2:
                r = R.layout.view_title_normal_item2;
                break;
            case 3:
                r = R.layout.view_title_normal_item3;
                break;
        }
        ImageView imageView = (ImageView) LayoutInflater.from(getContext()).inflate(r, null);
        imageView.setImageResource(resource);
        return imageView;
    }

    /**
     * @param colorIndex 1.黑色 2.蓝色
     */
    private TextView getTextBtn(String text, int index, int colorIndex) {
        @LayoutRes int r = 0;
        switch (index) {
            case 1:
                r = R.layout.view_title_normal_text_btn_item1;
                break;
            case 2:
                r = R.layout.view_title_normal_text_btn_item2;
                break;
        }
        TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(r, null);
        textView.setLayoutParams(new LinearLayout.LayoutParams(DensityUtil.dip2px(getContext(), 48), DensityUtil.dip2px(getContext(), 48)));
        textView.setText(text);

        switch (colorIndex) {
            default:
            case 1:
                textView.setTextColor(ContextCompat.getColor(getContext(), R.color.app_title_main));
                break;
            case 2:
                textView.setTextColor(ContextCompat.getColor(getContext(), R.color.app_main_color));
                break;
        }

        return textView;
    }

    /**
     * @param colorIndex 1.黑色 2.蓝色
     */
    private TextView getLeftTextBtn(String text, int index, int colorIndex) {
        @LayoutRes int r = 0;
        switch (index) {
            case 1:
                r = R.layout.view_title_normal_left_text_btn_item1;
                break;
        }
        TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(r, null);
        textView.setLayoutParams(new LinearLayout.LayoutParams(DensityUtil.dip2px(getContext(), 48), DensityUtil.dip2px(getContext(), 48)));
        textView.setText(text);

        switch (colorIndex) {
            default:
            case 1:
                textView.setTextColor(ContextCompat.getColor(getContext(), R.color.app_title_main));
                break;
            case 2:
                textView.setTextColor(ContextCompat.getColor(getContext(), R.color.app_main_color));
                break;
        }

        return textView;
    }

    public void setIcon1Listener(OnClickListener onClickListener) {
        if (ivIc1 != null)
            ivIc1.setOnClickListener(onClickListener);
    }

    public void setIcon2Listener(OnClickListener onClickListener) {
        if (ivIc2 != null)
            ivIc2.setOnClickListener(onClickListener);
    }

    public void setIcon3Listener(OnClickListener onClickListener) {
        if (ivIc3 != null)
            ivIc3.setOnClickListener(onClickListener);
    }

    public void setLeftIconListener(OnClickListener onClickListener) {
        if (ivBack != null)
            ivBack.setOnClickListener(onClickListener);
    }

    public void setLeftBtnListener(OnClickListener onClickListener) {
        if (tvLeftBtn != null)
            tvLeftBtn.setOnClickListener(onClickListener);
    }

    public void setTvBtn1Listener(OnClickListener onClickListener) {
        if (tvBtn1 != null)
            tvBtn1.setOnClickListener(onClickListener);
    }

    public void setTvBtn1Text(String text) {
        if (tvBtn1 != null) {
            tvBtn1.setText(text);
        }
    }

    public void setTvBtn2Text(String text) {
        if (tvBtn2 != null) {
            tvBtn2.setText(text);
        }
    }

    public void setTvBtn2Listener(OnClickListener onClickListener) {
        if (tvBtn2 != null)
            tvBtn2.setOnClickListener(onClickListener);
    }


    public ImageView getIcon1() {
        return ivIc1;
    }

    public ImageView getIcon2() {
        return ivIc2;
    }

    public ImageView getIcon3() {
        return ivIc3;
    }

    /**
     * 得到左边的布局
     */
    public ImageView getLeftIcon() {
        return ivBack;
    }

    public TextView getLeftBtn() {
        return tvLeftBtn;
    }

    public TextView getTvBtn1() {
        return tvBtn1;
    }

    public TextView getTvBtn2() {
        return tvBtn2;
    }

    public void addIcon1(int resource) {
        ivIc1 = getItemView(resource, 1);
        linearLayout.addView(ivIc1);
    }

    public void clearRightIcons() {
        if (linearLayout != null)
            linearLayout.removeAllViews();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 设置右上角的菜单的可见性
     */
    public void setMenuVisiblity(int visibility) {
        linearLayout.setVisibility(visibility);
    }
}
